//
//  XJCommentMsgCell.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/2.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJCommentMsgCell.h"
#import "YYLabel.h"
#import "NSAttributedString+YYText.h"


@interface XJCommentMsgCell()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *div1;
@property (nonatomic, strong) UIImageView *iconIv; //1.头像
@property (nonatomic, strong) YYLabel *nameTimeLb; //2. 张三 2018-06-08 20：36：17
@property (nonatomic, strong) UIButton *huifuTaButton; //3. 回复他 按钮

@property (nonatomic, strong) UILabel *reply_textLb; //4.张三回复的内容

@property (nonatomic, strong) UIView *div2;
@property (nonatomic, strong) YYLabel *namePublishTextLb; //5.都纷纷:"xxxxxx"




@end

@implementation XJCommentMsgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


#pragma mark - 一 lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}
-(UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
    }
    return _div1;
}
- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv = [UIImageView new];
        _iconIv.layer.masksToBounds = YES;
        _iconIv.layer.cornerRadius = 20;
    }
    return _iconIv;
}

- (YYLabel *)nameTimeLb {
    if (!_nameTimeLb) {
        _nameTimeLb = [YYLabel new];
    }
    return _nameTimeLb;
}

- (UIButton *)huifuTaButton {
    if (!_huifuTaButton) {
        _huifuTaButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_huifuTaButton setTitle:@"回复TA" forState:(UIControlStateNormal)];
        [_huifuTaButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_huifuTaButton addTarget:self action:@selector(huifuTaButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        _huifuTaButton.layer.masksToBounds = YES;
        _huifuTaButton.layer.cornerRadius = 10;
        _huifuTaButton.layer.borderColor = [UIColor blackColor].CGColor;
        _huifuTaButton.layer.borderWidth = 1.0;
    }
    return _huifuTaButton;
}


//需要高度自适应
- (UILabel *)reply_textLb {
    if (!_reply_textLb) {
        _reply_textLb = [[UILabel alloc] init];
        _reply_textLb.numberOfLines = 0;
    }
    return _reply_textLb;
}


- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
        _div2.backgroundColor = XJCOLOR_RGB(240, 240, 240);
    }
    return _div2;
}

//需要高度自适应
- (YYLabel *)namePublishTextLb {
    if (!_namePublishTextLb) {
        _namePublishTextLb = [[YYLabel alloc] init];
        _namePublishTextLb.numberOfLines = 0;
        
        // 使用富文本之后设置该属性没用
//        _namePublishTextLb.font = [UIFont systemFontOfSize:30.0];
    }
    return _namePublishTextLb;
}


#pragma mark - 二 init

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
    }
    
    return self;
}

- (void)setupView {

    [self.contentView addSubview:self.bgView];
    
    //第一部分 : 50
    [self.contentView addSubview:self.div1];
    [self.div1 addSubview:self.iconIv];
    [self.div1 addSubview:self.nameTimeLb];
    [self.div1 addSubview:self.huifuTaButton];

    
    //第二部分:40
    [self.contentView addSubview:self.reply_textLb];
    
    
    //第三部分 :需要高度自适应
    [self.contentView addSubview:self.div2];
    [self.div2 addSubview:self.namePublishTextLb];
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    
    CGFloat k5 = 5.0;
    CGFloat k10 = 10.0;
    CGFloat k15 = 15.0;
    
    //第一部分:高度固定:50
    [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(k15);
        make.top.equalTo(self.bgView.mas_top).offset(k5);
        make.height.mas_equalTo(50);
        make.right.equalTo(self.bgView.mas_right).offset(-k10);
    }];
    
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left);
        make.centerY.equalTo(self.div1.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [self.nameTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(k5);
        make.centerY.mas_equalTo(self.div1.mas_centerY);
        make.height.mas_equalTo(30);
    }];
    [self.huifuTaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.div1.mas_right);
        make.bottom.equalTo(self.div1.mas_bottom).offset(-10);
        make.size.mas_equalTo(CGSizeMake(65, 35));
    }];

    
    
    //第二部分:40
    [self.reply_textLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div1.mas_bottom).offset(k5);
        make.left.equalTo(self.bgView.mas_left).offset(50);
        make.right.equalTo(self.bgView.mas_right).offset(-k10);
        make.height.mas_equalTo(40);
    }];
    
    //第三部分 :需要高度自适应
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(50);
        make.right.equalTo(self.bgView.mas_right).offset(-k10);
        make.top.equalTo(self.reply_textLb.mas_bottom).offset(k5);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-k5);
    }];
    [self.namePublishTextLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2.mas_left).offset(k5);
        make.right.equalTo(self.div2.mas_right).offset(-k5);
        make.top.equalTo(self.div2.mas_top).offset(k5);
        make.bottom.equalTo(self.div2.mas_bottom).offset(-k5);
    }];
}


#pragma mark - 四 点击事件
- (void)huifuTaButtonAction:(UIButton *)button {
    [JRToast showWithText:@"未来巴扎:该评论已被删除"];
}


#pragma mark - 五 赋值
- (void)setModel:(XJCommentMsgModel *)model {
    NSURL *url = [NSURL URLWithString:model.reply_avatar];
    UIImage *placeholderImg = [UIImage imageNamed:@"default_head"];
    
    [self.iconIv sd_setImageWithURL:url placeholderImage:placeholderImg];
    
    
    
    
    
    UIColor *blueColor = [UIColor blueColor];
    UIColor *blackColor = [UIColor blackColor];
    UIColor *clearColor = [UIColor clearColor];
    
    NSString *timeStr = [NSString timeStrFromTimeStamp:[NSString stringWithFormat:@"%@",model.reply_time]];
    NSString *nameTimeStr = [NSString stringWithFormat:@"%@ %@",model.reply_nickname,timeStr];
    
    NSRange range1 = [nameTimeStr rangeOfString:model.reply_nickname];
    NSMutableAttributedString *attributeStr1 = [[NSMutableAttributedString alloc] initWithString:nameTimeStr attributes:@{NSForegroundColorAttributeName:blackColor,NSFontAttributeName:XJFONTSET_R(15.0)}];
    
    [attributeStr1 yy_setTextHighlightRange:range1 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
    }];
    self.nameTimeLb.attributedText = attributeStr1;
    
    
    
    self.reply_textLb.text = model.reply_text;
    
    
    
    
    NSString *namePublicshStr = [NSString stringWithFormat:@"%@:%@",model.publish_nickname,model.publish_text];
    NSRange range21 = [namePublicshStr rangeOfString:model.publish_nickname];
    NSMutableAttributedString *attributeStr2 = [[NSMutableAttributedString alloc] initWithString:namePublicshStr attributes:@{NSForegroundColorAttributeName:blackColor,NSFontAttributeName:XJFONTSET_R(15.0)}];
    [attributeStr2 yy_setTextHighlightRange:range21 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
    }];
    self.namePublishTextLb.attributedText = attributeStr2;
    
//    self.namePublishTextLb.text = namePublicshStr;
}


#pragma mark - 六 Private mehtod

/**
 
 // 求范围
 NSRange range1 = [str rangeOfString:childCommentModel.nickname];
 NSRange range2 = [str rangeOfString:childCommentModel.target_nickname];
 NSRange range3 = [str rangeOfString:childCommentModel.publish_time];
 
 
 // 富文本赋值
 NSMutableAttributedString *mAttributeStr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
 mAttributeStr.yy_font = [UIFont systemFontOfSize:14.0];
 mAttributeStr.yy_lineSpacing = 0;
 mAttributeStr.yy_alignment = NSTextAlignmentJustified;
 
 [mAttributeStr yy_setTextHighlightRange:range1 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
 XJLog(@"1111");
 }];
 
 [mAttributeStr yy_setTextHighlightRange:range2 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
 XJLog(@"222");
 }];
 
 [mAttributeStr yy_setTextHighlightRange:range3 color:grayColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
 XJLog(@"3333");
 }];
 
 self.commentLabel.attributedText = mAttributeStr;
 
 */
@end
